extends Node3D
class_name HealthBar3D

@onready var viewport: SubViewport = $SubViewport
@onready var sprite: Sprite3D = $Sprite3D
@onready var health_bar: ProgressBar = $SubViewport/HealthBar
@onready var mana_bar: ProgressBar = $SubViewport/ManaBar

var unit: Node3D

func _ready():
	# Create the viewport UI structure
	if not viewport:
		viewport = SubViewport.new()
		viewport.size = Vector2(100, 20)
		viewport.transparent_bg = true
		add_child(viewport)
	
	if not health_bar:
		health_bar = ProgressBar.new()
		health_bar.size = Vector2(100, 12)
		health_bar.position = Vector2(0, 0)
		health_bar.value = 100
		health_bar.show_percentage = false
		viewport.add_child(health_bar)
		
		# Style the health bar
		var health_style = StyleBoxFlat.new()
		health_style.bg_color = Color(0.8, 0.2, 0.2)
		health_style.corner_radius_top_left = 2
		health_style.corner_radius_top_right = 2
		health_style.corner_radius_bottom_left = 2
		health_style.corner_radius_bottom_right = 2
		health_bar.add_theme_stylebox_override("fill", health_style)
		
		var bg_style = StyleBoxFlat.new()
		bg_style.bg_color = Color(0.2, 0.2, 0.2, 0.8)
		bg_style.corner_radius_top_left = 2
		bg_style.corner_radius_top_right = 2
		bg_style.corner_radius_bottom_left = 2
		bg_style.corner_radius_bottom_right = 2
		health_bar.add_theme_stylebox_override("background", bg_style)
	
	if not mana_bar:
		mana_bar = ProgressBar.new()
		mana_bar.size = Vector2(100, 8)
		mana_bar.position = Vector2(0, 12)
		mana_bar.value = 0
		mana_bar.show_percentage = false
		viewport.add_child(mana_bar)
		
		# Style the mana bar
		var mana_style = StyleBoxFlat.new()
		mana_style.bg_color = Color(0.2, 0.4, 0.9)
		mana_style.corner_radius_top_left = 2
		mana_style.corner_radius_top_right = 2
		mana_style.corner_radius_bottom_left = 2
		mana_style.corner_radius_bottom_right = 2
		mana_bar.add_theme_stylebox_override("fill", mana_style)
		
		var mana_bg_style = StyleBoxFlat.new()
		mana_bg_style.bg_color = Color(0.1, 0.1, 0.2, 0.8)
		mana_bg_style.corner_radius_top_left = 2
		mana_bg_style.corner_radius_top_right = 2
		mana_bg_style.corner_radius_bottom_left = 2
		mana_bg_style.corner_radius_bottom_right = 2
		mana_bar.add_theme_stylebox_override("background", mana_bg_style)
	
	if not sprite:
		sprite = Sprite3D.new()
		sprite.billboard = BaseMaterial3D.BILLBOARD_ENABLED
		sprite.texture = viewport.get_texture()
		sprite.pixel_size = 0.01
		add_child(sprite)

func setup(target_unit: Node3D):
	unit = target_unit
	if unit.has_signal("health_changed"):
		unit.health_changed.connect(_on_health_changed)

func _on_health_changed(current: float, maximum: float):
	if health_bar:
		health_bar.max_value = maximum
		health_bar.value = current
		
		# Change color based on health percentage
		var health_percent = current / maximum
		var health_style = health_bar.get_theme_stylebox("fill")
		if health_style is StyleBoxFlat:
			if health_percent > 0.6:
				health_style.bg_color = Color(0.2, 0.8, 0.2)
			elif health_percent > 0.3:
				health_style.bg_color = Color(0.8, 0.8, 0.2)
			else:
				health_style.bg_color = Color(0.8, 0.2, 0.2)

func update_mana(current: float, maximum: float):
	if mana_bar:
		mana_bar.max_value = maximum
		mana_bar.value = current

func _process(_delta):
	if unit and is_instance_valid(unit):
		global_position = unit.global_position + Vector3(0, 2.5, 0)
		
		# Update mana if unit has it
		if unit.has_method("get_mana"):
			var mana_data = unit.get_mana()
			update_mana(mana_data.current, mana_data.max)